//index.js
//获取应用实例
const app = getApp()
// 引入SDK核心类
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 800,
    imgUrls: [],
    getimg: '',
    Hei: "", //轮播图容器高度
    goodslist: [],
    goodslistjf: [],
    navlist: [{
      "column_name": "企业介绍",
      "column_img": "https:\/\/yx.edenst.com\/upload\/banner\/20200611\/3574e704e9c25016d3df5c1ef59f6e16.png",
      "column_url": "\/pages\/about1\/about1"
    }, {
      "column_name": "信息登记",
      "column_img": "https:\/\/yx.edenst.com\/upload\/banner\/20200611\/034f810ab6a6c1cb8cc887042ceeeca9.png",
      "column_url": "\/pages\/inform\/inform"
    }, {
      "column_name": "积分商城",
      "column_img": "https:\/\/yx.edenst.com\/upload\/banner\/20200611\/02725fed8571c73a2953181ae790a236.png",
      "column_url": "\/pages\/shops\/shops"
    }, {
      "column_name": "服务中心",
      "column_img": "https:\/\/yx.edenst.com\/upload\/banner\/20200611\/7b242fc6d3065a9fa1d6d88e9b5b4fab.png",
      "column_url": "\/pages\/service\/service"
    }, {
      "column_name": "礼品中心",
      "column_img": "https:\/\/yx.edenst.com\/upload\/banner\/20200611\/3432e8ca863de031f0dd1b51a9aa1254.png",
      "column_url": "\/pages\/gift\/gift"
    }],
    currentCity: '',
    hotnews: [],
  },

  imgH: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width;
    var swiperH = winWid * imgh / imgw - 5 + "px" //等比设置swiper的高度。    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
      Hei: swiperH + 'px', //设置高度
    })
  },

  onLoad: function (options) {
    var that = this;

    // 获取当前城市
    qqmapsdk = new QQMapWX({
      key: '3TCBZ-5BZW6-ZT7ST-EH6A3-ZAC4O-OIBZY'
    });

    that.setData({
      getimg: app.d.hostImg
    });

    //获取banner
    wx.request({
      url: app.d.hostUrl + '/api/system/getGuanggaoList',
      method: 'post',
      data: {
        name:'首页'
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res);
        var imgUrls = res.data.data;
        that.setData({
          imgUrls: imgUrls,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000
        });
      },
    });

    //获取公告列表
    wx.request({
      url: app.d.hostUrl + '/api/system/getGonggaoList',
      method: 'post',
      data: {},
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res);
        var hotnews = res.data.data;
        that.setData({
          hotnews: hotnews,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000
        });
      },
    });

    //获取导航栏目
    wx.request({
      url: app.d.hostUrl + '/api/common/get_columnlist',
      method: 'post',
      data: {},
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res.data);
        var navlist = res.data.data;
        that.setData({
          navlist: navlist,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    });

    //获取商家列表
    wx.request({
      url: app.d.hostUrl + '/api/shop/getShopList',
      method: 'post',
      data: {},
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res);
        var shoplist = res.data.data;
        that.setData({
          shoplist: shoplist,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          icon:"none",
          duration: 2000
        });
      },
    });




  },

  onShow: function () {
    var that = this;
    qqmapsdk.reverseGeocoder({
      success: function (res) { //成功后的回调
        // console.log(res);
        var city = res.result.address_component.city;
        that.setData({
          currentCity: city
        });
      },
      fail: function (error) {
        // console.error(error);
      },
      complete: function (res) {
        console.log(res);
        var city = res.result.address_component.city;
        that.setData({
          currentCity: city,
        });
      }
    })
    // app.login();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var openid = wx.getStorageSync('openid');
    from: menu;
    return {
      title: '走吧商城',
      path: '/pages/onepage/onepage?openid=' + openid,
      success: function (res) {
        // console.log(openid);      
      },
      fail: function (res) {
        // console.log(res);
      }
    }
  },

  formSubmit(e) {
    let keyword = e.detail.value.skey;
    if (e.detail.value.skey.length == 0) {
      wx.showModal({
        content: "请输入关键字搜索!",
        showCancel: "true",
        confirmText: "我知道了",
        confirmcolor: "#ccc",
      })
      return false;
    }
    console.log(keyword);
    wx.reLaunch({
      url: '/pages/serlist/serlist?keyword=' + keyword
    })
    // console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },

})